import type {
  EmployeeBatchSetTagsForm,
  EmployeeForm,
  EmployeeImportForm,
  EmployeeQuery,
  EmployeeSensitiveQuery,
  EmployeeTransferForm,
  EmployeeVO,
} from './model';

import type { ID, IDS, PageResult } from '#/api/common';

import { commonExport, ContentTypeEnum } from '#/api/helper';
import { requestClient } from '#/api/request';

/**
 * 分页查询员工列表
 * @param data
 * @returns 员工列表
 */
export function employeePage(data?: EmployeeQuery) {
  return requestClient.post<PageResult<EmployeeVO>>('/employee/page', data);
}

/**
 * 查询员工列表
 * @param params
 * @returns 员工列表
 */
export function employeeList(params?: EmployeeQuery) {
  return requestClient.get<EmployeeVO[]>('/employee/list', { params });
}

/**
 * 查询员工详情
 * @param id id
 * @returns 员工详情
 */
export function employeeInfo(id: ID) {
  return requestClient.get<EmployeeVO>(`/employee/${id}`);
}

/**
 * 新增员工
 * @param data
 * @returns void
 */
export function employeeAdd(data: EmployeeForm) {
  return requestClient.postWithMsg<void>('/employee', data);
}

/**
 * 更新员工
 * @param data
 * @param withMsg
 * @returns void
 */
export function employeeUpdate(data: EmployeeForm, withMsg: boolean = true) {
  const msgOp = withMsg ? 'putWithMsg' : 'put';
  return requestClient[msgOp]<void>('/employee', data);
}

/**
 * 删除员工
 * @param id id
 * @returns void
 */
export function employeeRemove(id: ID | IDS) {
  return requestClient.deleteWithMsg<void>(`/employee/${id}`);
}

/**
 * 转移人员至其他跟进人
 * @param data
 */
export function employeeTransferFollowUp(data: EmployeeTransferForm) {
  return requestClient.postWithMsg<void>('/employee/transferFollowUp', data);
}

/**
 * 批量给人员打标签
 * @param data
 */
export function employeeBatchSetTags(data: EmployeeBatchSetTagsForm) {
  return requestClient.postWithMsg<void>('/employee/addLabel', data);
}

/**
 * 人员导入
 * @param data
 * @returns void
 */
export function employeeImport(data: EmployeeImportForm) {
  return requestClient.postWithMsg<void>('/employee/importExcel', data, {
    headers: {
      'Content-Type': ContentTypeEnum.FORM_DATA,
    },
    isTransformResponse: false,
  });
}

/**
 * 下载人员导入模板
 * @returns blob
 */
export function employeeDownloadImportTemplate(params?: EmployeeQuery) {
  return commonExport('/employee/importTemplate', params ?? {});
}

/**
 * 根据企业id查询员工，并根据职位分组
 * @param companyId 企业ID
 */
export function employeeQueryByCompanyIdGroupPosition(companyId: ID) {
  return requestClient.get<Record<string, EmployeeVO[]>>(
    '/employee/queryByCompanyIdGroupPosition',
    {
      params: { companyId },
    },
  );
}

/**
 * 人员邮箱导入匹配
 * @param data
 * @returns void
 */
export function employeeEmailMatch(data: EmployeeImportForm) {
  return requestClient.postWithMsg<void>('/employee/emailMatch', data, {
    headers: {
      'Content-Type': ContentTypeEnum.FORM_DATA,
    },
    isTransformResponse: false,
  });
}

/**
 * 人员手机导入匹配
 * @param data
 * @returns void
 */
export function employeeMobileMatch(data: EmployeeImportForm) {
  return requestClient.postWithMsg<void>('/employee/mobileMatch', data, {
    headers: {
      'Content-Type': ContentTypeEnum.FORM_DATA,
    },
    isTransformResponse: false,
  });
}

/**
 * 导入匹配并生成打标签任务
 * @param data
 */
export function employeeTagMatch(data: EmployeeImportForm) {
  return requestClient.postWithMsg<void>('/employee/addExcelLabelTask', data, {
    headers: {
      'Content-Type': ContentTypeEnum.FORM_DATA,
    },
    isTransformResponse: false,
  });
}

/**
 * 查询敏感信息
 * @param data
 */
export function employeeQuerySensitive(data: EmployeeSensitiveQuery) {
  return requestClient.post<string>('/employee/querySensitive', data);
}

/**
 * 查询敏感信息 - 隐式调用，不需权限
 * @param data
 */
export function employeeQuerySensitiveNoAuth(data: EmployeeSensitiveQuery) {
  return requestClient.post<string>('/employee/querySensitiveNoAuth', data);
}
